package jay.home.bet.dao;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

public class CounterDao
{
	private DataSource dataSource = null;

	
	public Map query(String eventid)
	{
		String sql = "select * from counter where eventid='" + eventid + "'";
		Map map = getTemplate().queryForMap(sql, null);
		return map;
	}
	
	public void count(String eventid, String counter, double money)
	{
		String sql = "";
		if(counter.contains("dy") || counter.contains("ds") || counter.contains("bqc") || counter.contains("bd") || counter.contains("rqs"))
			sql = "update counter set "+counter+"=" + counter + "+1" + ", s" + counter + "=s" + counter+"+" + money + " where eventid in (select ee.eventid from events ee where ee.compid=(select xx.compid from events xx where eventid='" + eventid + "'))";
		else
			sql = "update counter set "+counter+"=" + counter + "+1" + ", s" + counter + "=s" + counter+"+"  + money + "  where eventid='" + eventid + "'";
		//update counter set dx_home=33 where eventid in (select ee.eventid from events ee where ee.compid=(select xx.compid from events xx where eventid='22'))
		getTemplate().execute(sql);
	}
	
	public void update(String oddname, String eventid)
	{
		String sql = "call updateCounter('" + oddname + "', '" + eventid + "')";
		getTemplate().execute(sql);
	}
	
	/**
	 * 获取配置信息， 配置存放在config表里面。
	 * @param key string形式的键， 通过键可以查到值
	 * @return 值
	 */
	public String getConfig(String key)
	{
		String sql = "select c_value from config where c_key='" + key + "'";
		String r = (String)getTemplate().queryForObject(sql, String.class);
		return r;
	}
	
	/**
	 * 这个本应该搜在oddsDao里面的， 但是这条语句在ibatis中执行会报错， 就移到这来了。
	 * 执行过程是这样的:<br/>
	 * 先删除items中涉及到此event的数据项.然后判断每个尚未被处理order中是否有某个order对应的item数量为0, 如果是则将此order删除并且把order对应的amount返还给它的user.
	 * @param eventid
	 */
	public void deleteEvent(String eventid)
	{
//		TODO 搞了一个小时也米想出来.
		String sql = "";
		sql = "delete events, items from events left join items on events.eventid=items.eventid where events.eventid='"+eventid+"'";
//		getTemplate().execute(sql);
		sql = "delete events, items from events left join items on events.eventid=items.eventid where events.eventid='"+eventid+"'";
		getTemplate().execute(sql);
	}
	
	private JdbcTemplate template;
	private JdbcTemplate getTemplate()
	{
		if(template == null) template = new JdbcTemplate(getDataSource());
		return template;
	}
	

	public DataSource getDataSource()
	{
		return dataSource;
	}

	public void setDataSource(DataSource dataSource)
	{
		this.dataSource = dataSource;
	}

	/**
	 * 把涉及到此comp的item的result设置。
	 * @param compid
	 */
	public void updateItemByComp(String compid)
	{
		String sql;
		sql = "update items set result=(select r_full from comps where compid='" + compid + "') where items.eventid in (select ee.eventid from events ee where compid='" + compid + "') and items.half=false";
		getTemplate().execute(sql);
		sql = "update items set result=(select r_half from comps where compid='" + compid + "') where items.eventid in (select ee.eventid from events ee where compid='" + compid + "') and items.half=true";
		getTemplate().execute(sql);
	}
	

	public List<Map> getSatistic()
	{
		String sql;
		sql = "select * from satistic";

		List<Map> stc = (List<Map>)getTemplate().queryForList(sql);
		return stc;
	}
}
